const express = require("express")
const app = exress()
const fs = require('fs')
const multer = require('multer')

storage = multer.diskStorage({
    destination:__dirname+'../upload',
    filename:function (req,file,callback) {
        // filename.name
    let filename = file.originalname.split('.')
    callback(null,`${Data.now()}.${filename[filename.length-1]}`)
    }
})
upload = multer({
    sotrage
})
//静态文件中间件
app.use(express.static(__dirname+'../upload'))
//接受post传参,需要这样处理
app.use(express.json())
app.use(express.urlencoded({extended:true}))
// 跨域
app.use(function (req,res,next) {
    res.header('Access-Control-Allow-Origin','*');
    res.header('Access-Control-Allow-Credentials','true');
    res.header('Access-Control-Allow-Methods','POST');
    res.header('Set-cookie','mycookie=xiaoming');
    next();
})

// 路由开始
app.post('/upload',upload.single('file'),(req,res)=>{
    res.send('1')
})
app.get('/blob',(req,res)=>{
    fs.readFile('./20190302213702.png','binary',(err,filedata)=>{
        res.write(filedata,'binary');
        res.end()
    })
})
//以get方式访问
app.get('/',(req,res)=>{
    console.log(req.headers.cookie);
    res.send('Hello World!')  
})
//以post访问
app.post('/',(req,res)=>{
    console.log(req.query.callback);
    const str = `${req.query.callback}(${JSON.stringify({xiaoming:'100分'})})`;
    res.send(str)
})
// 监听端口
app.listen(334)